import 'package:flutter/material.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:percent_indicator/percent_indicator.dart';
import 'package:get/get.dart';

class LevelPointsProgressWidget extends StatelessWidget {
  LevelPointsProgressWidget({
    Key? key,
  }) : super(key: key);
  final UserController _userController = Get.find();

  @override
  Widget build(BuildContext context) {
    Log.debug('build normal LevelProgressIndicatorWidget');

    String? level = '1';
    int? progress = 0;
    String _currentLevel = 'Level'.tr;
    String _bookRead = 'Books Read:'.tr;
    String _totalPoints = 'Total Points:'.tr;

    return Obx(() {
      if (_userController.profile.value.userName != null) {
        if (_userController.profile.value.level != null) {
          try {
            level = _userController.profile.value.level
                .toString()
                .replaceAll('level', '');
            // progress = (_userController.profile.value.levelPercentage! / 100)
            //     .toDouble();
            progress = _userController.profile.value.levelPercentage ?? 0;
          } catch (error) {}
        }

        return Container(
          margin: EdgeInsets.symmetric(vertical: 20),
          child: Row(
            children: [
              Container(
                width: 400,
                child: new LinearPercentIndicator(
                  padding: EdgeInsets.all(2),
                  progressColor: Get.isDarkMode
                      ? Theme.of(context).primaryColorDark
                      : Color(0xffa3fca8),
                  animation: true,
                  lineHeight: 24.0,
                  animationDuration: 1500,
                  percent: (progress! / 100).toDouble(),
                  center: Text(
                    "$_currentLevel $level",
                    style: TextStyle(
                        letterSpacing: 1,
                        color: Get.isDarkMode ? Colors.black : Colors.blueGrey,
                        fontSize: 16.0,
                        fontWeight: FontWeight.w700),
                  ),
                  trailing: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      boxShadow: [
                        BoxShadow(
                          blurRadius: 0,
                          spreadRadius: 1,
                          color: Get.isDarkMode
                              ? Color(0xff666666)
                              : Colors.lightGreen,
                          offset: Offset(3, 3),
                        ),
                      ],
                    ),
                    margin: EdgeInsets.only(left: 5),
                    padding: const EdgeInsets.symmetric(
                      horizontal: 15,
                    ),
                    child: Text(
                      "$progress%",
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16.0,
                          fontWeight: FontWeight.w700),
                    ),
                  ),
                  barRadius: Radius.circular(16),
                  backgroundColor: Get.isDarkMode
                      ? Theme.of(context).primaryColor
                      : Theme.of(context).primaryColorLight,
                ),
              ),
              Spacer(),
              Row(
                children: [
                  Row(
                    children: [
                      Tooltip(
                        message: 'Total books you have read'.tr,
                        child: Icon(
                          Icons.menu_book_outlined,
                          color: Get.isDarkMode
                              ? Get.theme.primaryColorDark
                              : Colors.blueGrey,
                          size: 25.0,
                        ),
                      ),
                      SizedBox(width: 10),
                      Text(
                        '$_bookRead ${_userController.profile.value.numBooks.toString()}',
                        style: Theme.of(context).textTheme.labelSmall?.copyWith(
                            color: Get.isDarkMode
                                ? Get.theme.primaryColorDark
                                : Colors.black),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                  ),
                  SizedBox(width: 30),
                  Row(
                    children: [
                      Tooltip(
                        message: 'Total points you have earned'.tr,
                        child: Icon(
                          Icons.money_rounded,
                          color: Get.isDarkMode
                              ? Get.theme.primaryColorDark
                              : Colors.blueGrey,
                          size: 25.0,
                        ),
                      ),
                      SizedBox(width: 10),
                      Text(
                        '$_totalPoints ${_userController.profile.value.points.toString()}',
                        style: Theme.of(context).textTheme.labelSmall?.copyWith(
                              color: Get.isDarkMode
                                  ? Get.theme.primaryColorDark
                                  : Colors.black,
                            ),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ),
        );
      } else {
        return Text('');
      }
    });
  }
}
